//
//  PPAlbumContentCommentPermissionViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/11/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentCommentPermissionViewController.h"

#import "PPAlbumContentViewDataSource.h"

@interface PPAlbumContentCommentPermissionViewController () <UITableViewDelegate, UITableViewDataSource>
@property (readwrite, nonatomic, strong) NSNumber *permission;
@property (readwrite, nonatomic, strong) PPAbstractTableView *tableView;
@end

@implementation PPAlbumContentCommentPermissionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"评论权限";

    self.automaticallyAdjustsScrollViewInsets = YES;

    _tableView = [[PPAbstractTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];


    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor clearColor];
    headerView.frame = CGRectMake(0, 0, CGFLOAT_MIN, 16);
    _tableView.tableHeaderView = headerView;

    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.frame = CGRectMake(0, 0, CGFLOAT_MIN, CGFLOAT_MIN);
    _tableView.tableFooterView = footerView;

    __weak typeof(self)weakSelf = self;
    [self setNavigationLeftTitle:@"取消" action:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];

    [self setNavigationRightTitle:@"完成" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        weakSelf.dataSource.permission = weakSelf.permission;
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    _tableView.frame = self.view.bounds;
}

- (void)setDataSource:(PPAlbumContentViewDataSource *)dataSource {
    _dataSource = dataSource;

    _permission = dataSource.permission;

    [_tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 46;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIControl *headerView = [[UIControl alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    headerView.frame = CGRectMake(0, 0, tableView.width, [self tableView:tableView heightForHeaderInSection:section]);

    [headerView addTopBorderWithColor:kLineColor andWidth:kLineWidth];

    NSString *text = nil;
    PPAlbumCommentPermission permission = PPAlbumCommentNone;

    switch(section) {
        case 0: permission = PPAlbumCommentEnabled; text = @"允许评论"; break;

        case 1: permission = PPAlbumCommentDisabled; text = @"不允许评论"; break;

        default: break;
    }

    if(permission == _permission.integerValue) {
        UIImageView *indicatorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
        indicatorView.centerY = headerView.height/2;
        indicatorView.left = 16;
        [headerView addSubview:indicatorView];
    }

    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.textColor = [UIColor colorWithRGBHex:0x323232];
    label.font = [UIFont systemFontOfSize:16];
    [label sizeToFit];
    label.left = 44;
    label.centerY = headerView.height/2;
    [headerView addSubview:label];

    __weak typeof(self)weakSelf = self;
    [headerView setActionBlock:^(UIControl *control) {
        [weakSelf headerView:control withIndex:section permissionDidPressed:permission];
    } forControlEvents:UIControlEventTouchUpInside];

    return headerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)headerView:(UIControl *)headerView withIndex:(NSInteger)index permissionDidPressed:(PPAlbumCommentPermission)permission {
    _permission = @(permission);

    [_tableView reloadData];
}

@end
